package com.asia5b.a5mall.home.adapter

import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.asia5b.a5bmall.resource.ActCons
import com.asia5b.a5mall.home.R
import com.asia5b.a5mall.middleware.bind.ImageBindingHelper
import com.asia5b.a5mall.middleware.model.home.CategoryModel
import com.asia5b.a5mall.middleware.utils.push
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/**
 * Created by zhangyuncai on 2019/12/7.
 */
class CategoryRightAdapter(
        val fragment: Fragment,
        list: ArrayList<CategoryModel>
) : BaseQuickAdapter<CategoryModel, BaseViewHolder>(R.layout.item_categoryright_adapter, list) {
    override fun convert(helper: BaseViewHolder, item: CategoryModel) {
        helper.setText(R.id.tv_categoryright_title, item.showName())
        val adapter2 = CategoryRightChildAdapter(item.children ?: arrayListOf())
        val recyclerView = helper.getView<RecyclerView>(R.id.rv_categoryright)
        recyclerView.run {
            layoutManager = GridLayoutManager(context!!, 3)
            adapter = adapter2
        }
    }

    private inner class CategoryRightChildAdapter(
            list: ArrayList<CategoryModel>
    ) : BaseQuickAdapter<CategoryModel, BaseViewHolder>(R.layout.item_categoryright_child_adapter, list) {
        override fun convert(helper: BaseViewHolder, item: CategoryModel) {
            helper.setText(R.id.tv_category_name, item.showName())
            ImageBindingHelper.loadImage(helper.getView(R.id.iv_category_img), item.image)
            helper.getView<ConstraintLayout>(R.id.cons_category_vessel).setOnClickListener {
                fragment.push(ActCons.SearchGoodsActivity, {
                    it.withInt("category", item.catId)
                    it.withString("hint", item.showName())
                })
            }
        }

    }
}